<template>
  <div>
    <c-table-box ref="cTableBox" hide-func checkbox :other-raw="paramsRaw" :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <c-permission v-if="type==='all'">
          <SingleContract text="合同变更" opt-code="contractchange" change :sel-data="selections" @on-success="refreshTableData" />
          <SingleContract text="上传合同" opt-code="contract" upload-no-change :sel-data="selections" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
          <c-button type="primary" opt-code="exportApointReport" @click="exportReportVisible=true">委派待确认报表</c-button>

        </c-permission>
        <c-permission v-else-if="type==='waitAppoint'" code="appoint">
          <c-button type="primary" opt-code="appoint" @click="handleUnits">委派施工单位</c-button>

          <!-- <Units :sel-data="selections" opt-code="appoint" style="margin-right:5px;" @on-success="refreshTableData" /> -->
          <Abnormal :sel-data="selections" opt-code="submitException" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="exception" @click="handleException">异常处理</c-button>

        </c-permission>
        <c-permission v-else-if="type==='waitUploadContract'" code="contractupload">
          <SingleContract :sel-data="selections" opt-code="singleupload" @on-success="refreshTableData" />
          <Contract :sel-data="selections" opt-code="quantityupload" @on-success="refreshTableData" />
        </c-permission>
      </template>
    </c-table-box>
    <export-files
      v-if="exportVisible"
      title="导出"
      :config="exportConfig"
      :visible.sync="exportVisible"
      @success="handleExportSuccess"
    />
    <export-files
      v-if="exportReportVisible"
      title="委派待确认报表"
      :config="exportReportConfig"
      :visible.sync="exportReportVisible"
    />

  </div>
</template>
<script>
import { otherFiltersItem } from '@/utils'
import { handleException } from '@/api/decorate'

// import Units from './units'
import Abnormal from './abnormal'
import Contract from './contract'
import SingleContract from './singleContract'
export default {
  name: 'List',
  components: {
    // Units,
    Abnormal,
    Contract,
    SingleContract
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    },
    activeType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      exportVisible: false,
      exportReportVisible: false,
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/shopDecoration/pageSelect',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-perCode': 'appoint',
          'Permission-App': 'slm'
        },
        setting: []
      },
      setting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'decorationTaskCode',
          label: '装修任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'nature',
          label: '装修性质',
          minWidth: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_NATURE'
          }
        }, {
          prop: 'constState',
          label: '门店建设状态',
          minWidth: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_STATE'
          }
        },
        {
          prop: 'shopName',
          minWidth: 120,
          label: '所属门店'
        },
        {
          prop: 'clientName',
          minWidth: 120,
          label: '所属客户'
        },
        {
          prop: 'officeName',
          minWidth: 120,
          label: '所属办事处'
        },
        {
          prop: 'range',
          label: '装修范围',
          minWidth: 100,
          render: (h, r) => {
            return h('c-dict-range-view', { props: { dictTypeCode: 'DECORATION_RANGE', value: r.range }})
          }
        },
        {
          prop: 'schedule',
          label: '装修进度',
          minWidth: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'steps',
          label: '装修步骤',
          minWidth: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        {
          prop: 'state',
          label: '装修状态',
          minWidth: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'SHOP_DECORATION_STATE'
          }
        },
        {
          prop: 'isApplyReportOutTime',
          label: '装修申请是否超期',
          minWidth: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'isOutTimeUnaccept',
          label: '验收申请是否超期',
          minWidth: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'constUnitName',
          label: '施工单位',
          minWidth: 100
        },
        {
          prop: 'principal',
          label: '施工负责人',
          minWidth: 120
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          minWidth: 100
        },
        {
          prop: 'marketType',
          label: '报图类型',
          minWidth: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          minWidth: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        }
      ],
      waitUploadContractSetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'decorationTaskCode',
          label: '装修任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'shopName',
          label: '所属门店',
          width: 120
        },
        {
          prop: 'clientName',
          label: '所属客户',
          width: 120
        },
        {
          prop: 'officeName',
          label: '所属办事处',
          width: 120
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render: (h, r) => {
            return h('c-dict-range-view', { props: { dictTypeCode: 'DECORATION_RANGE', value: r.range }})
          }
        },
        {
          prop: 'schedule',
          label: '装修进度',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'steps',
          label: '装修步骤',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        {
          prop: 'state',
          label: '装修状态',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'SHOP_DECORATION_STATE'
          }
        },
        {
          prop: 'isApplyReportOutTime',
          label: '装修申请是否超期',
          width: 140,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'isOutTimeUnaccept',
          label: '验收申请是否超期',
          width: 140,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'constUnitName',
          label: '施工单位',
          width: 120
        },
        {
          prop: 'principal',
          label: '施工负责人',
          width: 120
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 120
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        }
      ],
      typeFilters: [],
      paramsRaw: {},
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    exportConfig() {
      return [{
        code: 'export',
        name: '施工单位委派',
        dataApi: '/slm-admin/api/v1/shopDecoration/sumExport',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id',
        headers: {
          'Permission-perCode': 'appoint',
          'Permission-App': 'slm'
        }
      }]
    },
    exportReportConfig() {
      return [{
        code: 'export',
        name: '施工单位待委派报表统计',
        dataApi: '/slm-admin/api/v1/shopDecoration/sumExportReport',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id',
        headers: {
          'Permission-perCode': 'appoint',
          'Permission-App': 'slm'
        }
      }]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
        this.setTableSetting(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.$nextTick(() => {
          this.$refs.cTableBox.initTableData()
        })
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    handleUnits() {
      if (!this.singleJudge(this.selections, '委派施工单位', true)) {
        return
      }
      this.$router.push({ name: 'Units', query: { id: this.selections[0].id }})
    },
    // 异常处理
    handleException() {
      if (!this.singleJudge(this.selections, '异常', true)) {
        return
      }
      this.$prompt('', '备注', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        inputPattern: /[\S]/,
        inputErrorMessage: '请输入'
      }).then(({ value }) => {
        handleException({ id: this.selections[0].id, exceptionRemark: value }).then(res => {
          if (res.success) {
            this.refreshTableData()
            this.$message.success('操作成功')
          }
        })
      }).catch(() => {
      })
    },
    // 设置table setting
    setTableSetting(v) {
      switch (v) {
        case 'waitUploadContract':
          this.$set(this.tableConfig, 'setting', this.waitUploadContractSetting)
          break
        default:
          this.$set(this.tableConfig, 'setting', this.setting)
      }
    },
    // 设置过滤条件
    setOtherFilters(v) {
      this.paramsRaw = {}
      switch (v) {
        case 'all':
          this.typeFilters = []
          break
        case 'waitAppoint':
          this.typeFilters = [
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('steps', 'B1', '=')
          ]
          break
        case 'waitUploadContract':
          this.paramsRaw = {}
          this.typeFilters = [
            otherFiltersItem('isEnd', '0', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            // otherFiltersItem('schedule', 'M', '<>')
            otherFiltersItem('steps', 'B3', '=')
          ]
          break
      }
    },
    // 刷新数据
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    selectionChange(v) {
      this.selections = v
    },
    // 删除
    clickRemoveHandle() {
      this.$confirm(`是否确认删除勾选的${this.selections.length}条数据？`, {
        type: 'warning',
        title: '删除'
      }).then(() => {
        console.log('删除成功！')
      })
    },
    // 跳转
    clickToRouterHandle(name) {
      this.$router.push({ name })
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: 'AppointDetail', query: { id: row.id, taskCode: row.decorationTaskCode }})
    },
    // 导出成功
    handleExportSuccess() {
      this.$message.success('导出成功！')
    }
  }
}
</script>

<style lang="scss" scoped>
 /deep/.el-checkbox__inner{
   border: 2px solid #3dc094;
 }
</style>
